#pragma once

#include "HashTable.h"
using namespace TL_Link;

namespace TL
{
	template<class K>
	struct HashFunc
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	template<>
	struct HashFunc<string>
	{
		size_t operator()(const string& key)
		{
			size_t ret = 0;
			for (auto ch : key)
			{
				ret += ch;
				ret *= 131;
			}
			return ret;
		}
	};
	
	template<class K, class Hash = HashFunc<K>>
	class unordered_set
	{
		struct SetKeyOfT
		{
			const K& operator()(const K& key)
			{
				return key;
			}
		};
	public:
		typedef typename TL_Link::HashTable<K, const K, SetKeyOfT, Hash>::iterator iterator;
		typedef typename TL_Link::HashTable<K, const K, SetKeyOfT, Hash>::const_iterator const_iterator;

		unordered_set() = default;

		unordered_set(const K& key)
		{
			insert(key);
		}

		unordered_set(const initializer_list<K>& il)
		{
			for (auto& key : il)
			{
				insert(key);
			}
		}

		void swap(unordered_set& us)
		{
			_ht.swap(us._ht);
		}

		iterator begin()
		{
			return _ht.begin();
		}

		iterator end()
		{
			return _ht.end();
		}

		const_iterator begin() const
		{
			return _ht.begin();
		}

		const_iterator end() const
		{
			return _ht.end();
		}

		iterator find(const K& key)
		{
			return _ht.find(key);
		}

		pair<iterator, bool> insert(const K& key)
		{
			return _ht.insert(key);
		}

		iterator erase(const K& key)
		{
			return _ht.erase(key);
		}

		size_t size()
		{
			return _ht.size();
		}
		
		bool empty()
		{
			return _ht.empty();
		}

		size_t count()
		{
			return _ht.count();
		}
	private:
		HashTable<K, const K, SetKeyOfT, Hash> _ht;
	};

	template<class K>
	void swap(unordered_set<K>& us1, unordered_set<K>& us2)
	{
		us1.swap(us2);
	}
}